<?php
/**
 * Created by PhpStorm.
 * User: sunjian
 * Date: 2018/1/21
 * Time: 21:57
 */

namespace backend\controllers;

use yii\data\Pagination;
use app\models\Customer;
use yii\web\Controller;
use app\models\Area;
use Yii;
use dosamigos\qrcode\QrCode;    //引入类


class CustomerController extends Controller
{
    public function actionCustomerAdd(){
        $areaModel = new Area();
        $areas = $areaModel->find()->where(['flag'=>1])->asArray()->all();
        return $this->render("customer-add",['areas'=>$areas]);
    }

    public function actionCustomerList(){

        $query = Customer::find()->where(['type' => 1]);
        $countQuery = clone $query;
        $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => '20']);
        $operators = $query->offset($pages->offset)
            ->limit($pages->limit)->asArray()
            ->all();


        return $this->render("customer-list",['operators'=>$operators,'p'=>$pages]);
    }
    public function actionAddCustomer(){
        $request = Yii::$app->request;
        if($request->isAjax){
            $return = [];
            $user = new Customer();
            $user->type=1;
            $user->createtime=time();

            $user->username=$request->post("username");
            $user->password=Yii::$app->security->generatePasswordHash("123456");
            $user->areaid=$request->post("areaid");
            $user->address=$request->post("address");
            $user->realname=$request->post("realname");
            $user->phone=$request->post("phone");

            if($user->save()){
                $return['status']=true;
            }else{
                $return['status']=false;
                $return['msg']=json_encode($user->errors);
            }
            return json_encode($return);
        }

    }

    public function actionCustomerEdit(){
        $request = Yii::$app->request;
        $id=$request->get("id");
        $areaModel = new Area();
        $areas = $areaModel->find()->where(['flag'=>1])->asArray()->all();

        $customer = Customer::findOne($id);
        return $this->render("customer-edit",['areas'=>$areas,"customer"=>$customer]);

    }

    public function actionEditCustomer(){
        $request = Yii::$app->request;
        if($request->isAjax){
            $return = [];
            $user=Customer::findOne($request->post('id'));
            $user->updatetime=time();

            $user->areaid=$request->post("areaid");
            $user->address=$request->post("address");
            $user->realname=$request->post("realname");
            $user->phone=$request->post("phone");

            if($user->save()){
                $return['status']=true;
            }else{
                $return['status']=false;
                $return['msg']=json_encode($user->errors);
            }
            return json_encode($return);
        }

    }

    public function actionCustomerReset(){
        $request = Yii::$app->request;
        if($request->isAjax){
            $id=$request->post("id");
            $customer = Customer::findOne($id);
            $customer->password=Yii::$app->security->generatePasswordHash("123456");
            $customer->updatetime=time();
            $return = [];
            if($customer->save()){
                $return['status']=true;
            }else{
                $return['status']=false;
                $return['msg']=json_encode($customer->errors);
            }
            return json_encode($return);
        }

    }

    public function actionCustomerDel(){
        $request = Yii::$app->request;
        if($request->isAjax){
            $id=$request->post("id");
            $customer = Customer::findOne($id);
            $return = [];
            if($customer->delete()){
                $return['status']=true;
            }else{
                $return['status']=false;
                $return['msg']=json_encode($customer->errors);
            }
            return json_encode($return);
        }

    }

    public function actionGetQrcodeNo(){
        $num = Customer::find()->where(['type' => 1])->count();
        echo $num;
    }

    public function actionQrcode(){

        $request = Yii::$app->request;
        $customer = Customer::find()->where(['type' => 1])->offset($request->get(no))
            ->limit(1)->asArray()
            ->one();
        QrCode::png($customer['id'],"qrcode/".$customer['realname'].".png");

        return ;

    }
}